
package com.yjh.admin.controller.chronic;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yjh.admin.entity.chronic.Equipment;
import com.yjh.admin.entity.chronic.EquipmentUser;
import com.yjh.admin.server.chronic.EquipmentService;
import com.yjh.admin.server.chronic.EquipmentUserService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import util.R;

import java.util.Objects;


/**
 * 设备列表
 *
 * @author GuoBin
 * @date 2020-08-05 17:47:36
 */
@ApiOperation("设备列表")
@RestController
@AllArgsConstructor
@RequestMapping("/equipment")
public class EquipmentController {

    private final EquipmentService equipmentService;

    private final EquipmentUserService equipmentUserService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param equipment 设备列表
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public R getEquipmentPage(Page page, Equipment equipment) {
        return  new R<>(equipmentService.page(page,Wrappers.query(equipment)));
    }


    /**
     * id查询设备列表
     * @param equipmentId id
     * @return R
     */
    @ApiOperation("通过id查询设备列表")
    @GetMapping("/getById")
    public R getById(@RequestParam("equipmentId") Integer equipmentId){
        return new R<>(equipmentService.getById(equipmentId));
    }

    /**
     * 新增设备列表
     * @param equipment 设备列表
     * @return R
     */
    @ApiOperation("新增设备列表")
    @PostMapping
    public R save(@RequestBody Equipment equipment){
        Assert.isNull(equipmentService.getOne(Wrappers.query(equipment)),"设备已存在！");
        return new R<>(equipmentService.save(equipment));
    }

    /**
     * 修改设备列表
     * @param equipment 设备列表
     * @return R
     */
    @ApiOperation("修改设备列表")
    @PutMapping
    public R updateById(@RequestBody Equipment equipment){
        return new R<>(equipmentService.updateById(equipment));
    }

    /**
     * 通过id删除设备列表
     * @param equipmentId id
     * @return R
     */
    @ApiOperation("删除设备列表")
    @DeleteMapping("/removeById")
    public R removeById(@RequestParam("equipmentId") Integer equipmentId){
        return new R<>(equipmentService.removeById(equipmentId));
    }

    /**
     * 查询全部
     * @param
     * @return R
     */
    @ApiOperation("查询全部")
    @GetMapping("/queryAll")
    public R queryAll(){
        return new R<>(equipmentService.list());
    }


    /**
     * 查看监测设备
     * @param
     * @return R
     */
    @ApiOperation("查看监测设备")
    @GetMapping("/queryEquipmentList")
    public R queryEquipment(@RequestParam("vipId") Integer vipId){
        return new R<>(equipmentUserService.queryEquipmentUserList(vipId));
    }


    /**
     * 修改设备状态
     * @param
     * @return R
     */
    @ApiOperation("修改设备状态")
    @GetMapping("/updateEquipmentStatus")
    public R updateEquipmentStatus(@RequestParam("vipId") Integer vipId,
                                   @RequestParam("equipmentId") Integer equipmentId,
                                   @RequestParam("status") String status){
        EquipmentUser equipmentUser = new EquipmentUser();
        equipmentUser.setEquipmentId(equipmentId);
        equipmentUser.setVipId(vipId);
        EquipmentUser equipmentUserQuery = equipmentUserService.getOne(Wrappers.query(equipmentUser));
        if(Objects.isNull(equipmentUserQuery)){
            equipmentUser.setStatus(status);
            equipmentUserService.save(equipmentUser);
        }else {
            equipmentUserQuery.setStatus(status);
            equipmentUserService.updateById(equipmentUserQuery);
        }
        return new R<>();
    }
}
